package com.macro.mall.tiny.modules.pms.controller;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.pms.model.PmsAd;
import com.macro.mall.tiny.modules.pms.service.PmsAdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 广告 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2024-11-14
 */
@RestController
@RequestMapping("/pms/pmsAd")
@Api(tags = "PmsAdController")
@Tag(name = "PmsAdController", description = "广告管理")
public class PmsAdController {
    @Autowired
    private PmsAdService adService;

    @ApiOperation(value = "添加广告")
    @PostMapping("/add")
    public CommonResult<Void> addAd(@RequestBody PmsAd ad) {
        adService.save(ad);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "修改广告")
    @PostMapping("/update")
    public CommonResult<Void> updateAd(@RequestBody PmsAd ad) {
        adService.updateById(ad);
        return CommonResult.success(null);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/delete")
    public CommonResult<Void> delete(@RequestBody List<Integer> ids) {
        adService.removeByIds(ids);
        return CommonResult.success(null);
    }

    @ApiOperation("前台获取广告万能接口")
    @PostMapping("/getAd")
    public CommonResult<List<PmsAd>> getAd(@RequestBody PmsAdDTO pmsAdDTO) {
        QueryWrapper<PmsAd> queryWrapper = new QueryWrapper<>();
        if (pmsAdDTO.getLocation() != null) {
            queryWrapper.eq("location", pmsAdDTO.getLocation());
        }
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("sort");
        return CommonResult.success(adService.list(queryWrapper));
    }

    @ApiOperation("后台分页获取万能广告万能接口")
    @PostMapping("/page")
    public CommonResult<IPage<PmsAd>> page(@RequestBody PmsAdDTO pmsAdDTO) {
        QueryWrapper<PmsAd> query = new QueryWrapper<>();
        if (pmsAdDTO.getId() != null) {
            query.eq("id", pmsAdDTO.getId());
        }
        if (pmsAdDTO.getName() != null) {
            query.like("name", pmsAdDTO.getName());
        }
        if (pmsAdDTO.getLocation() != null) {
            query.eq("location", pmsAdDTO.getLocation());
        }
        if (pmsAdDTO.getType() != null) {
            query.eq("type", pmsAdDTO.getType());
        }
        if (pmsAdDTO.getAppId() != null) {
            query.eq("app_id", pmsAdDTO.getAppId());
        }
        if (pmsAdDTO.getCreateTimeStart() != null) {
            query.ge("create_time", pmsAdDTO.getCreateTimeStart());
        }
        if (pmsAdDTO.getCreateTimeEnd() != null) {
            query.le("create_time", pmsAdDTO.getCreateTimeEnd());
        }
        if (pmsAdDTO.getExpiredTimeStart() != null) {
            query.ge("expired_time", pmsAdDTO.getExpiredTimeStart());
        }
        if (pmsAdDTO.getExpiredTimeEnd() != null) {
            query.le("expired_time", pmsAdDTO.getExpiredTimeEnd());
        }
        if (pmsAdDTO.getStatus() != null) {
            query.eq("status", pmsAdDTO.getStatus());
        }
        if (pmsAdDTO.getStatus() != null) {
            query.eq("status", pmsAdDTO.getStatus());
        }
        query.orderByDesc("create_time");

        return CommonResult.success(
                adService.page(
                        new Page<>(pmsAdDTO.getPageNum(), pmsAdDTO.getPageSize(), true),
                        query
                ));
    }

    @Data
    static class PmsAdDTO {

        @ApiModelProperty("后台查询参数 主键id")
        private Integer id;

        @ApiModelProperty("后台查询参数 广告名")
        private String name;

        @ApiModelProperty("通过查询参数 广告位置 0 首页 1 计算器")
        private Integer location;


        @ApiModelProperty("通过查询参数 广告类型 0 小程序 1 图片 ")
        private Integer type;

        @ApiModelProperty("后台查询参数 如果是小程序类型需要该参数进行跳转")
        private String appId;

        @ApiModelProperty("后台查询参数 创建时间起")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date createTimeStart;

        @ApiModelProperty("后台查询参数 创建时间止")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date createTimeEnd;

        @ApiModelProperty("后台查询参数 过期时间起")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date expiredTimeStart;

        @ApiModelProperty("后台查询参数 过期时间止")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date expiredTimeEnd;

        @ApiModelProperty("后台查询参数 状态 0不可用 1可用")
        private Integer status;

        @ApiModelProperty("后台查询参数 分页参数")
        private Integer pageNum = 0;

        @ApiModelProperty("后台查询参数 分页参数")
        private Integer pageSize = 10;
    }


}